package com.github.uniapp_tx_video_call_plugin;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;

import com.blankj.utilcode.util.ServiceUtils;
import com.taobao.weex.WXSDKInstance;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.liteav.CallService;
import com.tencent.liteav.login.ProfileManager;
import com.tencent.rtmp.TXLiveBase;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import io.dcloud.weex.AppHookProxy;

public class TxVideoAppProxy implements AppHookProxy {
    @Override
    public void onCreate(Application application) {

        Log.e("TxVideoAppProxy","onCreate");

        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

        TXLiveBase.setConsoleEnabled(true);

        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(application);
        strategy.setAppVersion(TXLiveBase.getSDKVersionStr());
        CrashReport.initCrashReport(application,strategy);

        TXLiveBase.getInstance().setLicence(application, TxVideoConfig.licenceUrl, TxVideoConfig.licenseKey);

        // 短视频licence设置
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            builder.detectFileUriExposure();
        }
        closeAndroidPDialog();

        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
//                if (ProfileManager.getInstance().isLogin()
//                        && !ServiceUtils.isServiceRunning(CallService.class)) {
//                    Intent intent = new Intent(activity, CallService.class);
//                    activity.startService(intent);
//                }
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });

        Log.e("TxVideoAppProxy","onCreate  end");
    }



    private void closeAndroidPDialog(){
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void onEventCallBack(MyEventObject eventObject){
        if(eventObject.event == MyEventObject.EVENT_IM){//IM相关事件
            TxVideoConfig.IMloginState = eventObject.state;
            Log.e("TxCallModel","IM login status changed =>"+TxVideoConfig.IMloginState);


        }
    }


}
